#include <algorithm>
#include <cstdio>
#define N 100001
using namespace std;
struct Edge {
	int x, y;
	friend bool operator<(Edge a, Edge b) {
		return a.y < b.y;
	}
} e[N];
int n, t;
int main() {
	// freopen("testdata.in", "r", stdin);
	// freopen("testdata.out", "w", stdout);
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; ++i) {
			scanf("%d %d", &e[i].x, &e[i].y);
		}
		sort(e + 1, e + n + 1);
		int last = e[1].y;
		for (int i = 2; i <= n; ++i) {
			if (e[i].x <= last && last <= e[i].y) continue;
			last = e[i].x;
		}
		printf("%d\n", last - e[1].y);
	}
	// fclose(stdin);
	// fclose(stdout);
	return 0;
}